/*
 * JFrPlayersMMG.java
 *
 * Created on 27 septembre 2007, 08:09
 */

package gotha;

import java.rmi.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.*;

/**
 *
 * @author  Administrateur
 */
public class JFrPlayersMMG extends javax.swing.JFrame{
    private static final long REFRESH_DELAY = 2000;
    private long lastComponentsUpdateTime = 0;

    
    private final int NAME_COL = 0;
    private final int FIRSTNAME_COL = 1;
    private final int RANK_COL = 2;
    private final int CORR_COL = 3;
    private final int RATING_COL = 4;
    private TournamentInterface tournament;
    /** Creates new form JFrPlayersMMG */
    public JFrPlayersMMG() {
        initComponents();
        setupRefreshTimer();
    }

    private void setupRefreshTimer(){
        ActionListener taskPerformer = new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                try {
                    if (tournament.getLastTournamentModificationTime() > lastComponentsUpdateTime) {
                        updateAllViews();
                    }
                } catch (RemoteException ex) {
                    Logger.getLogger(JFrGamesResults.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        new javax.swing.Timer((int) REFRESH_DELAY, taskPerformer).start();
    }


    /** Creates new form JFrPlayerManager */
    public JFrPlayersMMG(TournamentInterface tournament) throws RemoteException{
        this.tournament = tournament;
        
        initComponents();
        customInitComponents();
        setupRefreshTimer();

    }    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlInternal = new javax.swing.JPanel();
        pnlMMG0 = new javax.swing.JPanel();
        scpMMG0 = new javax.swing.JScrollPane();
        tblMMG0 = new javax.swing.JTable();
        txfNbPlayersMMG0 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnQuit = new javax.swing.JButton();
        pnlMMG1 = new javax.swing.JPanel();
        scpMMG1 = new javax.swing.JScrollPane();
        tblMMG1 = new javax.swing.JTable();
        txfNbPlayersMMG1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        pnlMMG2 = new javax.swing.JPanel();
        scpMMG2 = new javax.swing.JScrollPane();
        tblMMG2 = new javax.swing.JTable();
        txfNbPlayersMMG2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        pnlOthers = new javax.swing.JPanel();
        scpOthers = new javax.swing.JScrollPane();
        tblOthers = new javax.swing.JTable();
        txfNbOtherPlayers = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        btnM1_0 = new javax.swing.JButton();
        btn0_M1 = new javax.swing.JButton();
        btn0_1 = new javax.swing.JButton();
        btn1_0 = new javax.swing.JButton();
        btn1_2 = new javax.swing.JButton();
        btn2_1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnReset = new javax.swing.JButton();
        txfMMBar = new javax.swing.JTextField();
        btnHelp = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mac-Mahon Groups");
        setResizable(false);
        getContentPane().setLayout(null);

        pnlInternal.setLayout(null);

        pnlMMG0.setBorder(javax.swing.BorderFactory.createTitledBorder("Bar (Top Group)"));
        pnlMMG0.setLayout(null);

        tblMMG0.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "First name", "Rank", "Corr", "Rating"
            }
        ));
        scpMMG0.setViewportView(tblMMG0);

        pnlMMG0.add(scpMMG0);
        scpMMG0.setBounds(5, 40, 210, 255);

        txfNbPlayersMMG0.setEditable(false);
        txfNbPlayersMMG0.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        txfNbPlayersMMG0.setText("1234");
        pnlMMG0.add(txfNbPlayersMMG0);
        txfNbPlayersMMG0.setBounds(10, 20, 40, 20);

        jLabel2.setText("players");
        pnlMMG0.add(jLabel2);
        jLabel2.setBounds(60, 20, 70, 14);

        pnlInternal.add(pnlMMG0);
        pnlMMG0.setBounds(10, 10, 220, 300);

        btnQuit.setText("Quit");
        btnQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitActionPerformed(evt);
            }
        });
        pnlInternal.add(btnQuit);
        btnQuit.setBounds(400, 470, 370, 30);

        pnlMMG1.setBorder(javax.swing.BorderFactory.createTitledBorder("Bar + 1 (Super Group)"));
        pnlMMG1.setLayout(null);

        tblMMG1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "First name", "Rank", "Corr", "Rating"
            }
        ));
        scpMMG1.setViewportView(tblMMG1);

        pnlMMG1.add(scpMMG1);
        scpMMG1.setBounds(5, 40, 210, 255);

        txfNbPlayersMMG1.setEditable(false);
        txfNbPlayersMMG1.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        txfNbPlayersMMG1.setText("1234");
        pnlMMG1.add(txfNbPlayersMMG1);
        txfNbPlayersMMG1.setBounds(10, 20, 40, 20);

        jLabel3.setText("players");
        pnlMMG1.add(jLabel3);
        jLabel3.setBounds(60, 20, 70, 14);

        pnlInternal.add(pnlMMG1);
        pnlMMG1.setBounds(280, 10, 220, 300);

        pnlMMG2.setBorder(javax.swing.BorderFactory.createTitledBorder("Bar + 2 (Super-super Group)"));
        pnlMMG2.setLayout(null);

        tblMMG2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "First name", "Rank", "Corr", "Rating"
            }
        ));
        scpMMG2.setViewportView(tblMMG2);

        pnlMMG2.add(scpMMG2);
        scpMMG2.setBounds(5, 40, 210, 255);

        txfNbPlayersMMG2.setEditable(false);
        txfNbPlayersMMG2.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        txfNbPlayersMMG2.setText("1234");
        pnlMMG2.add(txfNbPlayersMMG2);
        txfNbPlayersMMG2.setBounds(10, 20, 40, 20);

        jLabel4.setText("players");
        pnlMMG2.add(jLabel4);
        jLabel4.setBounds(60, 20, 70, 14);

        pnlInternal.add(pnlMMG2);
        pnlMMG2.setBounds(550, 10, 220, 300);

        pnlOthers.setBorder(javax.swing.BorderFactory.createTitledBorder("Below bar"));
        pnlOthers.setLayout(null);

        tblOthers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "First name", "Rank", "Corr", "Rating"
            }
        ));
        scpOthers.setViewportView(tblOthers);

        pnlOthers.add(scpOthers);
        scpOthers.setBounds(5, 40, 210, 100);

        txfNbOtherPlayers.setEditable(false);
        txfNbOtherPlayers.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        txfNbOtherPlayers.setText("1234");
        pnlOthers.add(txfNbOtherPlayers);
        txfNbOtherPlayers.setBounds(10, 20, 40, 20);

        jLabel5.setText("players");
        pnlOthers.add(jLabel5);
        jLabel5.setBounds(60, 20, 70, 14);

        pnlInternal.add(pnlOthers);
        pnlOthers.setBounds(10, 350, 220, 150);

        btnM1_0.setText("^");
        btnM1_0.setMargin(new java.awt.Insets(2, 0, 2, 0));
        btnM1_0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnM1_0ActionPerformed(evt);
            }
        });
        pnlInternal.add(btnM1_0);
        btnM1_0.setBounds(50, 310, 30, 40);

        btn0_M1.setText("V");
        btn0_M1.setMargin(new java.awt.Insets(2, 0, 2, 0));
        btn0_M1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn0_M1ActionPerformed(evt);
            }
        });
        pnlInternal.add(btn0_M1);
        btn0_M1.setBounds(155, 310, 30, 40);

        btn0_1.setText(">>>");
        btn0_1.setMargin(new java.awt.Insets(2, 5, 2, 5));
        btn0_1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn0_1ActionPerformed(evt);
            }
        });
        pnlInternal.add(btn0_1);
        btn0_1.setBounds(230, 70, 50, 30);

        btn1_0.setText("<<<");
        btn1_0.setMargin(new java.awt.Insets(2, 5, 2, 5));
        btn1_0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1_0ActionPerformed(evt);
            }
        });
        pnlInternal.add(btn1_0);
        btn1_0.setBounds(230, 150, 50, 30);

        btn1_2.setText(">>>");
        btn1_2.setMargin(new java.awt.Insets(2, 5, 2, 5));
        btn1_2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1_2ActionPerformed(evt);
            }
        });
        pnlInternal.add(btn1_2);
        btn1_2.setBounds(500, 70, 50, 30);

        btn2_1.setText("<<<");
        btn2_1.setMargin(new java.awt.Insets(2, 5, 2, 5));
        btn2_1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn2_1ActionPerformed(evt);
            }
        });
        pnlInternal.add(btn2_1);
        btn2_1.setBounds(500, 150, 50, 30);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setLayout(null);

        jLabel1.setText("Mac-Mahon bar");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(150, 20, 110, 14);

        btnReset.setText("Reset All SMMS");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        jPanel1.add(btnReset);
        btnReset.setBounds(150, 50, 190, 30);

        txfMMBar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txfMMBarFocusLost(evt);
            }
        });
        jPanel1.add(txfMMBar);
        txfMMBar.setBounds(310, 20, 30, 20);

        pnlInternal.add(jPanel1);
        jPanel1.setBounds(280, 330, 490, 100);

        btnHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gotha/gothalogo16.jpg"))); // NOI18N
        btnHelp.setText("help");
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });
        pnlInternal.add(btnHelp);
        btnHelp.setBounds(280, 470, 110, 30);

        getContentPane().add(pnlInternal);
        pnlInternal.setBounds(0, 0, 780, 510);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txfMMBarFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txfMMBarFocusLost
        TournamentParameterSet tps = null;
        GeneralParameterSet gps = null;
        try {
            tps = tournament.getTournamentParameterSet();
            gps = tps.getGeneralParameterSet();
        } catch (RemoteException ex) {
            ex.printStackTrace();
            return;
        }   
        int oldGenMMBar = gps.getGenMMBar();
        int newGenMMBar = Player.convertKDToInt(this.txfMMBar.getText());
        if ((newGenMMBar > GeneralParameterSet.GEN_MM_BAR_MAX) 
            || (newGenMMBar < GeneralParameterSet.GEN_MM_BAR_MIN)
            || (newGenMMBar < gps.getGenMMFloor())){
            // Error. Keep old value
            txfMMBar.setText("" + Player.convertIntToKD(oldGenMMBar));
            return;
        } 
        if (newGenMMBar != oldGenMMBar){
            gps.setGenMMBar(newGenMMBar);
            try {
                tournament.setTournamentParameterSet(tps);
                this.tournamentChanged();
            } catch (RemoteException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_txfMMBarFocusLost

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
//        TournamentParameterSet tps = null;
        ArrayList<Player> alPlayers = null;
        try {
//            tps = tournament.getTournamentParameterSet();
            alPlayers = tournament.playersList();
        } catch (RemoteException ex) {
            ex.printStackTrace();
            return;
        }
        for (Player p : alPlayers){
            p.setSmmsCorrection(0);
            try {
                tournament.modifyPlayer(p, p);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (TournamentException ex) {
                ex.printStackTrace();
            }
        }

        this.tournamentChanged();

       
       
    }//GEN-LAST:event_btnResetActionPerformed

    private void btn2_1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn2_1ActionPerformed
        // get SelectedPlayers
        ArrayList<Player> alSelectedPlayers = selectedPlayersList(this.tblMMG2);
        if (alSelectedPlayers.size() == 0){
            JOptionPane.showMessageDialog(this, "Please, select at least one player in Bar + 2 table", "Message",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        for (Player p : alSelectedPlayers){
            decrementSMMS(p);
            try {
                tournament.modifyPlayer(p, p);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (TournamentException ex) {
                ex.printStackTrace();
            }
        }
        
        this.tournamentChanged();
        
    }//GEN-LAST:event_btn2_1ActionPerformed

    private void btn0_M1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn0_M1ActionPerformed
        // get SelectedPlayers
        ArrayList<Player> alSelectedPlayers = selectedPlayersList(this.tblMMG0);
        if (alSelectedPlayers.size() == 0){
            JOptionPane.showMessageDialog(this, "Please, select at least one player in Bar table", "Message",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        for (Player p : alSelectedPlayers){
            decrementSMMS(p);
            try {
                tournament.modifyPlayer(p, p);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (TournamentException ex) {
                ex.printStackTrace();
            }
        }
        this.tournamentChanged();

    }//GEN-LAST:event_btn0_M1ActionPerformed

    private void btn0_1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn0_1ActionPerformed
        // get SelectedPlayers
        ArrayList<Player> alSelectedPlayers = selectedPlayersList(this.tblMMG0);
        if (alSelectedPlayers.size() == 0){
            JOptionPane.showMessageDialog(this, "Please, select at least one player in Bar table", "Message",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        for (Player p : alSelectedPlayers){
            incrementSMMS(p);
            try {
                tournament.modifyPlayer(p, p);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (TournamentException ex) {
                ex.printStackTrace();
            }
        }
            this.tournamentChanged();
    }//GEN-LAST:event_btn0_1ActionPerformed

    
    private void btnM1_0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnM1_0ActionPerformed
        // get SelectedPlayers
        ArrayList<Player> alSelectedPlayers = selectedPlayersList(this.tblOthers);
        if (alSelectedPlayers.size() == 0){
            JOptionPane.showMessageDialog(this, "Please, select at least one player in Below Bar table", "Message",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        for (Player p : alSelectedPlayers){
            incrementSMMS(p);
            try {
                tournament.modifyPlayer(p, p);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (TournamentException ex) {
                ex.printStackTrace();
            }
        }        
        this.tournamentChanged();

    }//GEN-LAST:event_btnM1_0ActionPerformed

    private void btn1_0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn1_0ActionPerformed
        // get SelectedPlayers
        ArrayList<Player> alSelectedPlayers = selectedPlayersList(this.tblMMG1);
        if (alSelectedPlayers.size() == 0){
            JOptionPane.showMessageDialog(this, "Please, select at least one player in Bar + 1 table", "Message",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        for (Player p : alSelectedPlayers){
            decrementSMMS(p);
            try {
                tournament.modifyPlayer(p, p);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (TournamentException ex) {
                ex.printStackTrace();
            }

        }
        this.tournamentChanged();

    }//GEN-LAST:event_btn1_0ActionPerformed

    private void btn1_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn1_2ActionPerformed
        // get SelectedPlayers
        ArrayList<Player> alSelectedPlayers = selectedPlayersList(this.tblMMG1);
        if (alSelectedPlayers.size() == 0){
            JOptionPane.showMessageDialog(this, "Please, select at least one player in Bar + 1 table", "Message",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        for (Player p : alSelectedPlayers){
            incrementSMMS(p);
            try {
                tournament.modifyPlayer(p, p);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (TournamentException ex) {
                ex.printStackTrace();
            }
        }
        this.tournamentChanged();

    }//GEN-LAST:event_btn1_2ActionPerformed

    private void customInitComponents()throws RemoteException{       
        int w = JFrGotha.MEDIUM_FRAME_WIDTH;
        int h = JFrGotha.MEDIUM_FRAME_HEIGHT;
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((dim.width - w)/2, (dim.height -h)/2, w, h);

        setIconImage(Gotha.getIconImage());
        
        initColumnHeaders(this.tblMMG0);
        initColumnHeaders(this.tblMMG1);
        initColumnHeaders(this.tblMMG2);
        initColumnHeaders(this.tblOthers);

        
        updateComponents();
    }

    private void initColumnHeaders(JTable table){
         // Column names 
        TableColumnModel tcm = table.getColumnModel();
        tcm.getColumn(0).setHeaderValue("Name");
        tcm.getColumn(1).setHeaderValue("First name");
        tcm.getColumn(2).setHeaderValue("Rank");
        tcm.getColumn(3).setHeaderValue("Corr");
        tcm.getColumn(4).setHeaderValue("Rating");       
    }
    
    private void updateComponents(){
        TournamentParameterSet tps = null;
        ArrayList<Player> alPlayers = null;
        try {
            tps = tournament.getTournamentParameterSet();
            alPlayers = tournament.playersList();
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
        if(tps.tournamentType() != TournamentParameterSet.TYPE_MACMAHON){
            dispose();
            return;
        }
        
        pnlInternal.setVisible(true);
        GeneralParameterSet gps = tps.getGeneralParameterSet();

        this.txfMMBar.setText(Player.convertIntToKD(gps.getGenMMBar()));
        ArrayList<Player> alMMG0Players = new ArrayList<Player>();
        ArrayList<Player> alMMG1Players = new ArrayList<Player>();
        ArrayList<Player> alMMG2Players = new ArrayList<Player>();
        ArrayList<Player> alOtherPlayers = new ArrayList<Player>();
        
        for (Player p : alPlayers){
            if (p.smms(gps) == gps.getGenMMBar() - Gotha.MIN_RANK ){
                alMMG0Players.add(p);   
            }
            else if (p.smms(gps) == gps.getGenMMBar() - Gotha.MIN_RANK  + 1){
                alMMG1Players.add(p);   
            }
            else if (p.smms(gps) == gps.getGenMMBar() - Gotha.MIN_RANK  + 2){
                alMMG2Players.add(p);   
            }
            else{
                alOtherPlayers.add(p);
            }           
        }
        
        fillPlayersTable(alMMG0Players, this.tblMMG0);
        fillPlayersTable(alMMG1Players, this.tblMMG1);
        fillPlayersTable(alMMG2Players, this.tblMMG2);
        fillPlayersTable(alOtherPlayers, this.tblOthers);
        this.txfNbPlayersMMG0.setText("" + alMMG0Players.size());
        this.txfNbPlayersMMG1.setText("" + alMMG1Players.size());
        this.txfNbPlayersMMG2.setText("" + alMMG2Players.size());
        this.txfNbOtherPlayers.setText("" + alOtherPlayers.size());
    }

    private void fillPlayersTable(ArrayList<Player> alP, JTable tblP){
        DefaultTableModel model = (DefaultTableModel)tblP.getModel();       
        while (model.getRowCount() > 0) model.removeRow(model.getRowCount()-1);
        ArrayList<Player> alDP = new ArrayList<Player>(alP); // Displayed players
        PlayerComparator playerComparator = new PlayerComparator(PlayerComparator.RATING_ORDER);
        Collections.sort(alDP, playerComparator);
        for(Player p : alDP){
           Vector<String> row = new Vector<String>();
            row.add(p.getName());
            row.add(p.getFirstName());
            row.add(Player.convertIntToKD(p.getRank()));
            row.add("" + p.getSmmsCorrection());
            row.add("" + p.egfRating());
            model.addRow(row);
        }
    }

    private ArrayList<Player> selectedPlayersList(JTable tbl){
        ArrayList<Player> alSelectedPlayers = new ArrayList<Player>();
       
        // gather selected players into alSelectedPlayers
        for ( int iRow = 0; iRow < tbl.getModel().getRowCount(); iRow++){
            if (tbl.isRowSelected(iRow)){
                String name = (String)tbl.getModel().getValueAt(iRow, NAME_COL);
                String firstName = (String)tbl.getModel().getValueAt(iRow, FIRSTNAME_COL);
                Player p = null;
                try {
                    p = tournament.getPlayerByKeyString(name + firstName);
                } catch (RemoteException ex) {
                    ex.printStackTrace();
                }
                alSelectedPlayers.add(p);
            }
        }
        return alSelectedPlayers;
    }
    
    private void incrementSMMS(Player p){
        int smmsCorr = p.getSmmsCorrection();
        if (smmsCorr < 2) p.setSmmsCorrection(smmsCorr + 1);
        else{
            int answer = JOptionPane.showConfirmDialog(this, 
                    p.fullName() + "'s_rank_is_actually" + Player.convertIntToKD(p.getRank())
                    + "\n" + "Do_you_want_to_set_it_to" + Player.convertIntToKD(p.getRank() + 1),
                    "Message",
                    JOptionPane.YES_NO_OPTION);
            if (answer == JOptionPane.YES_OPTION)
                p.setRank(p.getRank() + 1);                  
        }
    }    
    
    private void decrementSMMS(Player p){
        int smmsCorr = p.getSmmsCorrection();
        if (smmsCorr > -1) p.setSmmsCorrection(smmsCorr - 1);
        else{
            int answer = JOptionPane.showConfirmDialog(this, 
                    p.fullName() + "'s_rank_is_actually" + Player.convertIntToKD(p.getRank())
                    + "\n" + "Do_you_want_to_set_it_to" + Player.convertIntToKD(p.getRank() - 1),
                    "Message",
                    JOptionPane.QUESTION_MESSAGE);
            if (answer == JOptionPane.YES_OPTION)
                p.setRank(p.getRank() - 1);                  
        }
    }        
 
        
    private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitActionPerformed
        dispose();
    }//GEN-LAST:event_btnQuitActionPerformed

    private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
        Gotha.displayGothaHelp("Mac-Mahon groups frame");
}//GEN-LAST:event_btnHelpActionPerformed
    
 
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn0_1;
    private javax.swing.JButton btn0_M1;
    private javax.swing.JButton btn1_0;
    private javax.swing.JButton btn1_2;
    private javax.swing.JButton btn2_1;
    private javax.swing.JButton btnHelp;
    private javax.swing.JButton btnM1_0;
    private javax.swing.JButton btnQuit;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pnlInternal;
    private javax.swing.JPanel pnlMMG0;
    private javax.swing.JPanel pnlMMG1;
    private javax.swing.JPanel pnlMMG2;
    private javax.swing.JPanel pnlOthers;
    private javax.swing.JScrollPane scpMMG0;
    private javax.swing.JScrollPane scpMMG1;
    private javax.swing.JScrollPane scpMMG2;
    private javax.swing.JScrollPane scpOthers;
    private javax.swing.JTable tblMMG0;
    private javax.swing.JTable tblMMG1;
    private javax.swing.JTable tblMMG2;
    private javax.swing.JTable tblOthers;
    private javax.swing.JTextField txfMMBar;
    private javax.swing.JTextField txfNbOtherPlayers;
    private javax.swing.JTextField txfNbPlayersMMG0;
    private javax.swing.JTextField txfNbPlayersMMG1;
    private javax.swing.JTextField txfNbPlayersMMG2;
    // End of variables declaration//GEN-END:variables

    public void tournamentChanged(){
        try {
            if (!tournament.isOpen()){
                dispose();
                return;
            }
            tournament.setLastTournamentModificationTime(tournament.getCurrentTournamentTime());
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }

        updateAllViews();
    }

    private void updateAllViews(){
        try {
            this.lastComponentsUpdateTime = tournament.getCurrentTournamentTime();
            setTitle("Mac-Mahon Groups. " + tournament.getTournamentParameterSet().getGeneralParameterSet().getShortName());
        } catch (RemoteException ex) {
            Logger.getLogger(JFrPlayersMMG.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        updateComponents();
    }
}
